require 'MVVM.Sources.View.Adaptor'
require 'MVVM.Sources.View.Adaptors.ListAdaptor'

ScrollSnapIndexAdaptor = class('ScrollSnapIndexAdaptor', Adaptor)

function ScrollSnapIndexAdaptor:ctor(gameObject, propertyName, binder)
    self.component = gameObject:GetComponent('HorizontalScrollSnap')
    self:BindProperty(propertyName, binder)
    binder:RegisterEvent(function(viewModel, property)
        self.component.OnSelectChange = function(index)
            if index + 1 ~= property.Value then
                property.Value = index + 1
            end
        end

    end, function()
        self.component.OnSelectChange = nil
    end, propertyName)
end

function ScrollSnapIndexAdaptor:OnViewModelValueChanged(oldValue, newValue)
    if newValue == oldValue then
        return
    end
    
    if newValue == self.component.CurrentPage + 1 then
        return
    end


    self.component:GoToScreen(newValue - 1)
    self.component.StartingScreen = newValue - 1
end